package com.adopapa.nezhacloud.register.service;

import java.util.Date;
import java.util.UUID;

import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.core.RabbitTemplate.ConfirmCallback;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.adopapa.nezhacloud.ddd.service.BaseService;
import com.adopapa.nezhacloud.register.conf.RabbitConfig;

@Component
public class DataSender extends BaseService implements ConfirmCallback {// ,
	// ApplicationListener<AOpplicationReadyEvent>
	private RabbitTemplate rabbitTemplate;

	@Autowired
	public DataSender(RabbitTemplate rabbitTemplate) {
		this.rabbitTemplate = rabbitTemplate;
		this.rabbitTemplate.setConfirmCallback(this);
	}

	public void send() {
		String content = "北京 " + new Date();
		System.out.println("sender: " + content);
		CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());
		this.rabbitTemplate.convertAndSend(RabbitConfig.EXCHANGE, RabbitConfig.ROUTINGKEY, //
				content, correlationData);
	}

	public <T> void send(T params) {
		CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());

		this.rabbitTemplate.setConfirmCallback(this);
		this.rabbitTemplate.convertAndSend(RabbitConfig.EXCHANGE, RabbitConfig.ROUTINGKEY, //
				params, correlationData);
	}

	@Override
	public void confirm(CorrelationData correlationData, boolean ack, String cause) {
		logger.info("correlationData: {}, ack: {}, cause: {}", correlationData, ack, cause);

	}

	// @Override
	// public void onApplicationEvent(ApplicationReadyEvent event) {
	// this.rabbitTemplate.setConfirmCallback(this);
	//
	// }

}
